package com.fairytask.ui.mainUnit
{
	import com.fairytask.data.Modulator;
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.UnitUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * 精练 
	 * @author zhut
	 * 
	 */
	public class RefineUI extends UnitUI
	{
		private var _model                   :Modulator = Modulator.instance;	
		
		public var imgBG:MovieClip;			//	背景
		public var equipInfoBG:MovieClip		
		public var img:Clip;				
		public var equipName:Label;			//装备的名称
		public var leftBG:MovieClip;			//左边属性显示框的背景
		public var leftLab:Label;				
		public var leftPropLab:Label;			//左边显示框的label
		public var rightBG:MovieClip;			//右边显示的背景
		public var rightLab:Label;				
		public var rightPropLab:Label;			//右边显示的label
		public var insertBtn1:Button;			//放入幸运符的按钮1
		public var insertBG1:MovieClip;		//幸运符的背景框
		public var insertBtn2:Button;			//放入幸运符的按钮2
		public var insertBG2:MovieClip;		//幸运符的背景框
		public var sliverLab:Label;
		public var successLab:Label;
		public var insertBtn3:Button;			//放入幸运符的按钮3
		public var insertBG3:MovieClip;		//幸运符的背景框
		public var luckyPic1:Clip;
		public var luckyPic2:Clip;
		public var luckyPic3:Clip;
		public var refineBtn:Button;
		
		private var _point:Point;				//用于存放已经点击后的装备的Id如果是不同的就进行刷新，如果不是就不操作
		private var starBG:Clip;
		private var starMode:Clip;
		private var _arr:Array=[];
		private var _arrStr:Array=[];
		private static var _self:RefineUI;
		private var strProp:String;
		private var _equip:ViewObject;
		private var _pos:int;
		public var _bindArr:Array = [0,0,0];
		
		private var _flag:Boolean	=	false;		//用于判断是否有装备
		private var _name:String	=	"";			//用于存放装备的名字
		private var _propSilver:int;				//人物身上的钱数
		private var _sivlverNow:int;				//当前需要的钱数
		private var _unbind	:Array	=	[];		//绑定属性，用于解绑
		
		private var currColor:int;				//装备的品质
		private var colorStr:int;				//装备的真实的品质
		
		public static function getInstance():RefineUI
		{
			if(_self==null)
			{
				_self=new RefineUI();
			}
			return _self;
		}
		public function RefineUI()
		{
			initEvent();
		}
		
			
		
		private function onAdd(key:String,val:int):void
		{
			switch(key)
			{
				case "Star":
					initInfo(val);
					break;
				case "Color":
					colorStr = val;
					break;
				case "ConfigID":
					img.source=ResConfig.iconPath+(StaticIni.getIniVar(val,"Icon"))+ResConfig.iconImg;
					//添加tips
					img.addEventListener(MouseEvent.ROLL_OVER,function(evt:MouseEvent):void
					{
						TipsManage.ShowItemMaxTips(val,_equip);
					});
					img.addEventListener(MouseEvent.ROLL_OUT,function(evt:MouseEvent):void
					{
						ToolTips.hide();
					});
					_name = StaticIni.getIniVar(val,"Name");
					break;
					
			}
		}

		//初始化监听事件
		private function initEvent():void
		{
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_SHOW_LUCKY,getLucky);
			//收到成功的消息，对装备的效果显示
			//_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_REFINE_SUCCEED,effect);
			//绑定装备的等级，进行对银两颜色的显示
			//bindManager.BindViewProp(model.scene.GetView(ViewDefine.VIEWPORT_PACKAGE),"Silver",getCapital);
			refineBtn.addEventListener(MouseEvent.CLICK,onRefine);
			insertBtn1.addEventListener(MouseEvent.CLICK,onInsert1);
			insertBtn2.addEventListener(MouseEvent.CLICK,onInsert2);
			insertBtn3.addEventListener(MouseEvent.CLICK,onInsert3);
			luckyPic1.name	=	"1";
			luckyPic2.name	=	"2";
			luckyPic3.name	=	"3";
			luckyPic1.addEventListener(MouseEvent.CLICK,removeLuck);
			luckyPic2.addEventListener(MouseEvent.CLICK,removeLuck);
			luckyPic3.addEventListener(MouseEvent.CLICK,removeLuck);
			luckyPic1.visible	=	false;
			luckyPic2.visible	=	false;
			luckyPic3.visible	=	false;
			
		}
		/*private function showLucky(evt:CustomToClientEvent):void
		{
			this.addChild(LuckyRefUI.getInstance());
		}*/
		private function onInsert1(e:MouseEvent):void
		{
			if(_flag)
			{
				_pos=1;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUTORMMSG_LUCKY_CHARM);
			}
			
		}
		private function onInsert2(e:MouseEvent):void
		{
			if(_flag)
			{
				_pos=2;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUTORMMSG_LUCKY_CHARM);
			}
			
		}
		private function onInsert3(e:MouseEvent):void
		{
			if(_flag)
			{
				_pos=3;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUTORMMSG_LUCKY_CHARM);
			}
			
		}
		private function onRefine(e:MouseEvent):void
		{
			if(_flag)
			{
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REFINE_EQUIP,_equip.objId,_bindArr[0],_bindArr[1],_bindArr[2]);
				_bindArr=[0,0,0];
				luckyPic1.visible	=	false;
				luckyPic2.visible	=	false;
				luckyPic3.visible	=	false;
			}
			
		}
		
		/**
		 * 传入当前装备
		 * @param equip
		 * @param viewID
		 * 
		 */
		public function setEquip(equip:ViewObject,viewID:int):void
		{
			
			if(equip==null)
			{
				return;
			}
			if(_point==equip.objId)
			{
				return;
			}
			_point = equip.objId;
			leftPropLab.htmlStrByBorder="";
			rightPropLab.htmlStrByBorder="";
			_bindArr = [0,0,0];
			luckyPic1.visible	=	false;
			luckyPic2.visible	=	false;
			luckyPic3.visible	=	false;
			_flag	=	true;
			_equip=equip;
			if(equip==null)
			{
				return;
			}
		}
		
		/**
		 *获取玩家拥有的银两 
		 * @param key
		 * @param val
		 * 
		 */
		private function getCapital(key:String,val:int):void
		{
			_propSilver = val;
			if(_sivlverNow!=0)
			{
				sliverLab.htmlStrByBorder=StaticIni.getSwfLangStr("1000000078")+"<br>"+StaticIni.getSwfLangStrVar(_propSilver<_sivlverNow?"1000000037":"1000000034",[_sivlverNow])+StaticIni.getSwfLangStr("1000000067");	
			}
		}
		
		private function onAddName(key:String,val:int):void
		{
			//根据不同的装备等级来显示不同的名字
			var headName:String	=	""
			//headName	=	TipsTools.getRefineName(val) + _name;
			//equipName.htmlStrByBorder = TipsTools.onlyColor(headName,colorStr);
		}
		//添加星形图标
		private function initInfo(num:int):void
		{
			
			if(starBG!=null)
			{
				windowBG.removeChild(starBG);
			}
			starBG=new Clip();
			starBG.x=30;
			starBG.y=100;
			starBG.height=80;
			starBG.width=200;
			starBG.visible = true;
			windowBG.addChild(starBG);
			for(var i:int=1;i<=num;i++)
			{
				createStarMode(i);	
			}
			
		}
		
		private function createStarMode(i:int):void
		{
			var row:int;
			var col:int;
			if(i%10!=0)
			{
				row=int(i/10)+1;
			}
			else
			{
				row=int(i/10);
			}
			if(i%10!=0)
			{
				col=int(i%10);
			}
			else
			{
				col=10
			}
			starMode=new Clip();
			starMode.source=ResConfig.pngResPath+"tips_star2.png";			
			starMode.width = 23;
			starMode.height = 23;
			starMode.x=(col-1)*25;
			starMode.y=(row-1)*25;
			starBG.addChild(starMode);
		}
		//获取幸运符
		/*public function getLucky(e:CustomToClientEvent):void
		{
			LuckyRefUI.getInstance().setPos(_pos);
			LuckyRefUI.getInstance().setPic(e.pList);
			if(!LuckyRefUI.getInstance().visible)
			{
				LuckyRefUI.getInstance().visible	=	true;
			}
			this.addChild(LuckyRefUI.getInstance());
		}*/
		
		/**
		 * 获取装备属性属性
		 * @param e
		 * 
		 */
		/*public function getProp(e:CustomToClientEvent):void
		{
			sliverLab.visible	=	true;
			if(starBG!=null)
			{
				starBG.visible = true;
			}
			leftPropLab.htmlStrByBorder="";
			rightPropLab.htmlStrByBorder="";
			var i:int;
			var n:int=0;
			strProp="";
			for(i=n+1;i<e.pList[0]*2+n+1;i+=2)
			{
				addLabelStr(e.pList[i],e.pList[i+1]);
			}
			leftPropLab.htmlStrByBorder=StaticIni.getSwfLangStrVar("1000000033",[strProp]);
			strProp="";
			n=e.pList[0]*2+1;
			for(i=n+1;i<n+1+e.pList[n]*2;i+=2)
			{
				addLabelStr(e.pList[i],e.pList[i+1]);
			}
			rightPropLab.htmlStrByBorder=StaticIni.getSwfLangStrVar("1000000033",[strProp]);
			n=n+1+e.pList[n]*2+1;
			_sivlverNow	=	e.pList[n-1];
			sliverLab.htmlStrByBorder=StaticIni.getSwfLangStr("1000000078")+"<br>"+StaticIni.getSwfLangStrVar(_propSilver<_sivlverNow?"1000000037":"1000000034",[_sivlverNow])+StaticIni.getSwfLangStr("1000000067");
			successLab.htmlStrByBorder=StaticIni.getSwfLangStr("1000000079")+StaticIni.getSwfLangStr("1000000070")+"<br>"+e.pList[n]/100+"%";
		}*/
		
		/**
		 *添加强化符 
		 * @param pos
		 * @param val
		 * 
		 */		
		public function getLuckRes(pos:int,val:String):void
		{
			switch(pos)
			{
				case 1:
					luckyPic1.source=ResConfig.iconPath+StaticIni.getIniVar(val,"Icon")+ResConfig.iconImg;
					if(!luckyPic1.visible)
					{
						luckyPic1.visible = true;
					}
					_bindArr[0]=int(val);
					break;
				
				case 2:
					luckyPic2.source=ResConfig.iconPath+StaticIni.getIniVar(val,"Icon")+ResConfig.iconImg;
					if(!luckyPic2.visible)
					{
						luckyPic2.visible = true;
					}
					_bindArr[1]=int(val);
					break;
				
				case 3:
					luckyPic3.source=ResConfig.iconPath+StaticIni.getIniVar(val,"Icon")+ResConfig.iconImg;
					if(!luckyPic3.visible)
					{
						luckyPic3.visible = true;
					}
					_bindArr[2]=int(val);
					break;
				default:
					break;
			}
			addTips(pos,val);
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_REFINE,_equip.objId,_bindArr[0],_bindArr[1],_bindArr[2]);
			LuckyRefUI.getInstance().visible=false;
		}
		
		/**
		 * 添加不同的tips
		 * @param pos
		 * @param val
		 * 
		 */		
		
		private function addTips(pos:int,val:String):void
		{
			switch(pos)
			{
				case 1:
					//添加tips
					luckyPic1.addEventListener(MouseEvent.ROLL_OVER,function(evt:MouseEvent):void
					{
						TipsManage.ShowItemTips(int(val),true);
					});
					luckyPic1.addEventListener(MouseEvent.ROLL_OUT,function(evt:MouseEvent):void
					{
						ToolTips.hide();
					});
					break;
				case 2:
					//添加tips
					luckyPic2.addEventListener(MouseEvent.ROLL_OVER,function(evt:MouseEvent):void
					{
						TipsManage.ShowItemTips(int(val),true);
					});
					luckyPic2.addEventListener(MouseEvent.ROLL_OUT,function(evt:MouseEvent):void
					{
						ToolTips.hide();
					});
					break;
				case 3:
					//添加tips
					luckyPic3.addEventListener(MouseEvent.ROLL_OVER,function(evt:MouseEvent):void
					{
						TipsManage.ShowItemTips(int(val),true);
					});
					luckyPic3.addEventListener(MouseEvent.ROLL_OUT,function(evt:MouseEvent):void
					{
						ToolTips.hide();
					});
					break;
				default:
					break;
			}
				
		}
		
		
		/**
		 *取消放入框显示的幸运符 
		 * 位置判断是哪个框内的幸运符
		 * @param pos
		 * 
		 */		
		private function removeLuck(evt:MouseEvent):void
		{
			var pos:int	=	int(evt.target.name);
			switch(pos)
			{
				case 1:
					if(luckyPic1.visible)
					{
						luckyPic1.visible=false;
					}
					LuckyRefUI.getInstance().modifyNum(_bindArr[0]);
					_bindArr[0]= 0;
					break;
				case 2:
					if(luckyPic2.visible)
					{
						luckyPic2.visible=false;
					}
					LuckyRefUI.getInstance().modifyNum(_bindArr[1]);
					_bindArr[1]= 0;
					break;
				case 3:
					if(luckyPic3.visible)
					{
						luckyPic3.visible=false;
					}
					LuckyRefUI.getInstance().modifyNum(_bindArr[2]);
					_bindArr[2] = 0;
					break;
			}
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PREPARE_REFINE,_equip.objId,_bindArr[0],_bindArr[1],_bindArr[2]);
		}
		
		
		public function clear():void
		{
			if(starBG != null)
			{
				starBG.visible = false;
				leftPropLab.htmlStrByBorder = "";
				rightPropLab.htmlStrByBorder= "";
				sliverLab.htmlStrByBorder   = "";
				successLab.htmlStrByBorder  = "";
				img.clear();
				equipName.htmlStrByBorder="";
				sliverLab.visible	=	false;
				for each(var itemBind:Array in _unbind)
				{
					bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
				}
				luckyPic1.visible	=	false;
				luckyPic2.visible	=	false;
				luckyPic3.visible	=	false;
				_flag = false;
				_bindArr = [];
				LuckyRefUI.getInstance().visible	=	false;
				_point = null;
			}
		}
	
		private function addLabelStr(name:String,val:int):void
		{
			var str:String;
			str=StaticIni.getSwfLangStrVar(name,[val]);
			strProp+=str+"<br>";
		}
		
		/**
		 *精炼成功后显示特效 
		 * 
		 */		
		/*private function effect(evt:CustomToClientEvent):void
		{
			TweenMax.to(img,2, {glowFilter:{color:0xFFFFFF, alpha:0.7, blurX:30, blurY:30, strength:2, quality:1},repeat:1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}},onComplete:onFlashOver});
			
		}*/
		
		private function onFlashOver():void
		{
			img.filters = null;
		}
	}
}